package demogeneric;

/**
 * @Author 12629
 * @Description：
 */
/*class MyArray {

    public Object[] array = new Object[3];

    public void setArray(int pos,Object val) {
        array[pos] = val;
    }

    public Object getVal(int pos) {
        return array[pos];
    }

}*/

/**
 * 泛型类
 * 1. <T> 表示 当前类是一个泛型类
 * @param <T>
 */
class MyArray<T /*extends Number*/> {

    public Object[] array = new Object[3];

    public void setArray(int pos,T val) {
        array[pos] = val;
    }

    public T getVal(int pos) {
        return (T)array[pos];
    }
}
public class Test {
    public static void main(String[] args) {

    }
    public static void main2(String[] args) {
        MyArray<Integer> myArray1 = new MyArray<Integer>();
        myArray1.setArray(0,19);
        //myArray1.setArray(1,"hello");
        myArray1.setArray(1,29);//在编译的时候 自动进行类型检查

        Integer val = myArray1.getVal(1);//在编译的时候 自动进行类型转换
        System.out.println(val);

        MyArray<String> myArray2 = new MyArray<>();
        myArray2.setArray(0,"hello");
        myArray2.setArray(1,"world");

        String str = myArray2.getVal(0);
        System.out.println(str);

    }

    public static void main1(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setArray(0,10);
        myArray.setArray(1,"hello");

        String ret = (String) myArray.getVal(1);

    }
}
